//
//  WPDiseaseTypesVC.m
//  wobingwoyi
//
//  Created by WGP on 16/9/5.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPDiseaseTypesVC.h"
#import "WPDiseaseTypeFirstCVC.h"
#import "WPDiseaseTypesSecondCVC.h"
#import "WPGoldCoinVC.h"
#import "WPDiseaseTypesDetailVC.h"
#import "WPMineLoginVC.h"
#import "WPCoinTopUp.h"
#import "WPSearchVC.h"
#import "WPReadingViewCollectionViewCell.h"
#import "WPReadingViewCollectionViewCellVC.h"
#import "WPReadingViewCollectionViewChargeCellVC.h"
#import "WPNewDiseaseInstructionView.h"

@interface WPDiseaseTypesVC ()<UICollectionViewDelegate,UICollectionViewDataSource,WPDiseaseTypesCVCDelegate,WPReadingViewCollectionViewCellVCDelegate,WPReadingViewCollectionViewChargeCellVCDelegate>

@property(nonatomic,strong)UIView *windowView;
@property(nonatomic,strong)UIButton *coverBtn;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIView *navBarView;
@property(nonatomic,strong)UIView *statusBarView;
@property(nonatomic,strong)UIButton *backBtn;
@property(nonatomic,strong)UIButton *searchBtn;

@property(nonatomic,strong)UIView *titleView;
@property(nonatomic,strong)WPBtnNoHighLight *indivalBtn;
@property(nonatomic,strong)WPBtnNoHighLight *famousBtn;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)WPBtnNoHighLight *currentBtn;
@property(nonatomic,strong)UIView *sliderView;
@property(nonatomic,assign)CGFloat averageWidth;
@property(nonatomic,assign)BOOL onceToken;
@property(nonatomic,assign)BOOL onceTokenSecond;

@property(nonatomic,assign)int userGlod;
@property(nonatomic,assign)int recordgold;

@property(nonatomic,strong)WPMRModel *selectedModel;
@property(nonatomic,strong)UIAlertView *alertView;

@property(nonatomic,copy)NSString *navTitle;

@end

@implementation WPDiseaseTypesVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.view.backgroundColor = GlobalBgColor;
    
    self.titleLabel.text = self.navTitle;
    
    [self setupUI];
}

-(void)setupUI
{
    [self statusBarView];
    [self navBarView];
    [self titleLabel];
    [self backBtn];
    [self searchBtn];
    [self titleView];
    [self collectionView];
}

#pragma mark - 懒加载
-(UIView *)statusBarView
{
    if (_statusBarView == nil)
    {
        _statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        _statusBarView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_statusBarView];
        
    }
    return _statusBarView;
}

-(UIView *)navBarView
{
    if (_navBarView == nil)
    {
        _navBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
        _navBarView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_navBarView];
        
        UIImageView *lineView = [UIImageView new];
        lineView.backgroundColor = GLobalLineColor;
        [_navBarView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.5);
            make.left.right.bottom.equalTo(_navBarView);
        }];
    }
    return  _navBarView;
}

-(UILabel *)titleLabel
{
    if (_titleLabel == nil)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = RGB_SAME(51);
        _titleLabel.text = self.diseaseTitle;
        _titleLabel.font = [UIFont systemFontOfSize:18];
        [self.navBarView addSubview:_titleLabel];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.navBarView);
        }];
    }
    return _titleLabel;
}

-(UIButton *)backBtn
{
    if (_backBtn == nil)
    {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setBackgroundImage:[UIImage imageNamed:@"back_normal"] forState:UIControlStateNormal];
        [_backBtn setBackgroundImage:[UIImage imageNamed:@"back_highlight"] forState:UIControlStateHighlighted];
        [_backBtn sizeToFit];
        [_backBtn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [self.navBarView addSubview:_backBtn];
        
        [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.navBarView);
            make.left.equalTo(self.navBarView).offset(10);
        }];
    }
    return _backBtn;
}

-(UIButton *)searchBtn
{
    if (_searchBtn == nil)
    {
        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchBtn setBackgroundImage:[UIImage imageNamed:@"fiveDiseasesSearch"] forState:UIControlStateNormal];
        [_searchBtn addTarget:self action:@selector(searchBtnDidClick) forControlEvents:UIControlEventTouchDown];
        [self.navBarView addSubview:_searchBtn];
        
        [_searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.navBarView);
            make.right.equalTo(self.navBarView).offset(-10);
        }];
    }
    return _searchBtn;
}

-(UIView *)titleView
{
    if (_titleView == nil)
    {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 44)];
        _titleView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_titleView];
        
        self.indivalBtn = [self buttonWith:@"普通病例"];
        self.currentBtn = self.indivalBtn;
        self.currentBtn.selected = YES;
        self.famousBtn = [self buttonWith:@"精华病例"];
        
        [_titleView addSubview:self.indivalBtn];
        [_titleView addSubview:self.famousBtn];
        
        UIImageView *lineView = [UIImageView new];
        lineView.backgroundColor = GLobalLineColor;
        [_titleView addSubview:lineView];
        
        CGFloat titleWidth = [self.indivalBtn.titleLabel.text sizeWithMaxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:15].width + [self.famousBtn.titleLabel.text sizeWithMaxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:15].width;
        self.averageWidth = (SCREEN_WIDTH-titleWidth)/3;
        
        [self.indivalBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_titleView);
            make.left.equalTo(_titleView).offset(self.averageWidth);
        }];
        
        [self.famousBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_titleView);
            make.right.equalTo(_titleView).offset(-self.averageWidth);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.5);
            make.bottom.left.right.equalTo(_titleView);
            
        }];
        
        self.sliderView = [[UIView alloc] initWithFrame:CGRectMake(self.averageWidth, _titleView.bounds.size.height-1.5, titleWidth*0.5, 1.5)];
        self.sliderView.backgroundColor = GlobalColor;
        [_titleView addSubview:self.sliderView];
        
    }
    return _titleView;
}

-(UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemH = SCREEN_HEIGHT-CGRectGetMaxY(self.titleView.frame);
        layout.itemSize = CGSizeMake(SCREEN_WIDTH, itemH);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleView.frame), SCREEN_WIDTH, itemH) collectionViewLayout:layout];
        _collectionView.backgroundColor = GlobalBgColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = 0;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerClass:[WPDiseaseTypeFirstCVC class] forCellWithReuseIdentifier:@"WPDiseaseTypeFirstCVC"];
        [_collectionView registerClass:[WPDiseaseTypesSecondCVC class] forCellWithReuseIdentifier:@"WPDiseaseTypesSecondCVC"];
        [_collectionView registerClass:[WPReadingViewCollectionViewCell class] forCellWithReuseIdentifier:@"WPReadingViewCollectionViewCell"];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

-(void)setDiseaseTitle:(NSString *)diseaseTitle
{
    _diseaseTitle = diseaseTitle;
    
    if ([diseaseTitle isEqualToString:@"category1"])
    {
        self.navTitle = @"皮肤病";
    }
    else if([diseaseTitle isEqualToString:@"category2"])
    {
        self.navTitle = @"慢性病";
    }
    else if([diseaseTitle isEqualToString:@"category3"])
    {
        self.navTitle = @"血液病";
    }
    else if([diseaseTitle isEqualToString:@"category4"])
    {
        self.navTitle = @"肿瘤病";
    }
    else if([diseaseTitle isEqualToString:@"category5"])
    {
        self.navTitle = @"婴幼儿疾病";
    }
    else
    {
        self.navTitle = diseaseTitle;
    }
}

#pragma mark - 数据源方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    WPReadingViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WPReadingViewCollectionViewCell" forIndexPath:indexPath];
    //    移除上一个控制器
    [cell.tvc.view removeFromSuperview];
    
    if (indexPath.item == 0)
    {
        WPReadingViewCollectionViewCellVC *tvc = [WPReadingViewCollectionViewCellVC new];
        //    传递控制器
        tvc.isFiveDiseases = self.isFiveDiseases;
        tvc.isPartOfHuman = self.isPartOfHuman;
        tvc.diseaseCategoryId = self.diseaseTitle;
        cell.tvc = tvc;
        tvc.delegate = self;
        //    如果一个控制器的View作为另一个控制器的子控件那么这个控制器也应该作为另一个控制器的子控制器
        [self addChildViewController:tvc];
    }
    else
    {
        WPReadingViewCollectionViewChargeCellVC *tvc = [WPReadingViewCollectionViewChargeCellVC new];
        //    传递控制器
        tvc.isFiveDiseases = self.isFiveDiseases;
        tvc.isPartOfHuman = self.isPartOfHuman;
        tvc.diseaseCategoryId = self.diseaseTitle;
        cell.tvc = tvc;
        tvc.delegate = self;
        //    如果一个控制器的View作为另一个控制器的子控件那么这个控制器也应该作为另一个控制器的子控制器
        [self addChildViewController:tvc];
    }
    
    return cell;
}

#pragma mark - 代理方法
-(void)indivalCaseCellDidClick:(WPDiseaseTypesCVC *)cell model:(WPMRModel *)model
{
//    if ([WPUserAccountViewModel sharedInstance].userLogin)
//    {
        WPDiseaseTypesDetailVC *rirVC = [WPDiseaseTypesDetailVC new];
        rirVC.model = model;
        [self.navigationController pushViewController:rirVC animated:YES];
//    }
//    else
//    {
//        
//        [[WPUserAccountViewModel sharedInstance] loginTipWith:^{
//            
//            WPMineLoginVC *vc = [WPMineLoginVC new];
//            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
//            vc.successLoginBlock = ^
//            {
////                登陆成功
//                WPDiseaseTypesDetailVC *rirVC = [WPDiseaseTypesDetailVC new];
//                rirVC.model = model;
//                [self.navigationController pushViewController:rirVC animated:YES];
//            };
//        }];
//        
//    }
}

-(void)famousCellCellDidClick:(WPDiseaseTypesCVC *)cell model:(WPMRModel *)model
{
    self.selectedModel = model;
    
    if ([WPUserAccountViewModel sharedInstance].userLogin)
    {
        NSString *url = @"record/getGoldNum.do";
        NSDictionary *dict = @{@"recordId":@(model.id)};
        
        [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if ([responseObject[@"result"] isEqualToString:@"success"])
            {
                if ([responseObject[@"detail"][@"status"] intValue])
                {
                    
                    self.hidesBottomBarWhenPushed = YES;
                    WPReadingInvidialRanking *rirVC = [WPReadingInvidialRanking new];
                    rirVC.model = model;
                    [self.navigationController pushViewController:rirVC animated:YES];
                    self.hidesBottomBarWhenPushed = NO;
                }
                else
                {
                    self.recordgold = [responseObject[@"detail"][@"recordGold"] intValue];
                    self.userGlod = [responseObject[@"detail"][@"userGold"] intValue];
                    
                    [self showGoldInfomation];
                }
            }
            else
            {
                //            考虑到账号过期的情况
                if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
                {
                    //                账号过期
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                    self.alertView = alertView;
                    [alertView show];
                }
                else
                {
                    [self showErrorInfoWithStatus:@"网络错误"];
                }
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [self showErrorInfoWithStatus:@"网络错误"];
        }];
    }
    else
    {

        [[WPUserAccountViewModel sharedInstance] loginTipWith:^{

            WPMineLoginVC *vc = [WPMineLoginVC new];
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
            vc.successLoginBlock = ^
            {
                //登陆成功
//                WPDiseaseTypesDetailVC *rirVC = [WPDiseaseTypesDetailVC new];
//                rirVC.model = model;
//                [self.navigationController pushViewController:rirVC animated:YES];
            };
        }];
        
    }
    
}

//重新登陆的代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
        if (buttonIndex == 0)
        {
            WPMineLoginVC *vc = [WPMineLoginVC new];
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
            
            
            if (self.alertView == alertView)
            {
 
                vc.successLoginBlock = ^
                {
                    [self famousCellCellDidClick:nil model:self.selectedModel];
                };
            }
            else
            {
                //        通知阅读量
                NSString *url = @"record/readRecord.do";
                NSDictionary *dict = @{@"recordId":@(self.selectedModel.id)};
                
                [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
                    
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    
                    if([responseObject[@"result"] isEqualToString:@"success"])
                    {
                        [self coverBtnDidClick];
                        
                        [self showSuccessInfoWithStatus:@"付费成功"];
                        
                        self.hidesBottomBarWhenPushed = YES;
                        WPReadingInvidialRanking *rirVC = [WPReadingInvidialRanking new];
                        rirVC.model = self.selectedModel;
                        [self.navigationController pushViewController:rirVC animated:YES];
                        self.hidesBottomBarWhenPushed = NO;
                    }
                    else
                    {
                       [self showErrorInfoWithStatus:@"付费失败,请稍后重试"];
                    }
                    
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    
                    [self showErrorInfoWithStatus:@"网络错误"];
                    
                }];

            }
        }
}

-(void)showGoldInfomation
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIButton *coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.coverBtn = coverBtn;
    coverBtn.frame = [UIScreen mainScreen].bounds;
    coverBtn.backgroundColor = RGBA_COLOR(51, 51, 51, 0.5);
    [coverBtn addTarget:self action:@selector(coverBtnDidClick) forControlEvents:UIControlEventTouchDown];
    [window addSubview:coverBtn];
    
    UIView *windowView = [UIView new];
    self.windowView = windowView;
    windowView.layer.cornerRadius = 6;
    windowView.layer.masksToBounds = YES;
    windowView.backgroundColor = [UIColor whiteColor];
    [window addSubview:windowView];
    
    [windowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(window).offset(47);
        make.right.equalTo(window).offset(-47);
        make.height.mas_equalTo(295);
        make.top.equalTo(window).offset(186*K_SCALE_FULL);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = GLobalFontColor;
    titleLabel.text = @"确认购买";
    [titleLabel sizeToFit];
    [windowView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(windowView).offset(26);
        make.centerX.equalTo(windowView);
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setBackgroundImage:[UIImage imageNamed:@"chaBtn"] forState:UIControlStateNormal];
    [windowView addSubview:cancelBtn];
    [cancelBtn addTarget:self action:@selector(coverBtnDidClick) forControlEvents:UIControlEventTouchDown];
    [cancelBtn sizeToFit];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(windowView).offset(8);
        make.right.equalTo(windowView).offset(-8);
    }];
    
    UILabel *goodsLabel = [UILabel new];
    goodsLabel.font = [UIFont systemFontOfSize:14];
    goodsLabel.textColor = GLobalFontColor;
    goodsLabel.text = @"购买物件：电子病历";
    [goodsLabel sizeToFit];
    [windowView addSubview:goodsLabel];
    
    [goodsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(windowView).offset(35);
        make.top.equalTo(titleLabel.mas_bottom).offset(26);
    }];
    
    NSString *purchasePriceStr = [NSString stringWithFormat:@"购买价格：%zd医票",self.recordgold];
    NSMutableAttributedString *purchasePrice = [[NSMutableAttributedString alloc] initWithString:purchasePriceStr];
    [purchasePrice addAttributes:@{NSForegroundColorAttributeName:GlobalColor} range:[purchasePriceStr rangeOfString:[NSString stringWithFormat:@"%zd医票",self.recordgold]]];
    
    UILabel *purchasePriceLabel = [UILabel new];
    purchasePriceLabel.font = [UIFont systemFontOfSize:14];
    purchasePriceLabel.textColor = GLobalFontColor;
    purchasePriceLabel.attributedText = purchasePrice;
    [purchasePriceLabel sizeToFit];
    [windowView addSubview:purchasePriceLabel];
    
    [purchasePriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(goodsLabel);
        make.top.equalTo(goodsLabel.mas_bottom).offset(20);
    }];
    
    
    NSString *currentGoldStr = [NSString stringWithFormat:@"当前医票：%zd医票",self.userGlod];
    NSMutableAttributedString *currentGoldAtt = [[NSMutableAttributedString alloc] initWithString:currentGoldStr];
    [currentGoldAtt addAttributes:@{NSForegroundColorAttributeName:GlobalColor} range:[currentGoldStr rangeOfString:[NSString stringWithFormat:@"%zd医票",self.userGlod]]];
    
    UILabel *currentGlodLabel = [UILabel new];
    currentGlodLabel.font = [UIFont systemFontOfSize:14];
    currentGlodLabel.textColor = GLobalFontColor;
    currentGlodLabel.attributedText = currentGoldAtt;
    [currentGlodLabel sizeToFit];
    [windowView addSubview:currentGlodLabel];
    
    [currentGlodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(purchasePriceLabel);
        make.top.equalTo(purchasePriceLabel.mas_bottom).offset(20);
    }];
    
    NSString *title = (self.recordgold > self.userGlod) ? @"医票不足，请充值" : @"确认购买";
    UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [okBtn setBackgroundImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal];
    [okBtn setBackgroundImage:[UIImage imageNamed:@"highLight"] forState:UIControlStateHighlighted];
    [okBtn addTarget:self action:@selector(okBtnDidClick) forControlEvents:UIControlEventTouchDown];
    [okBtn setTitle:title forState:UIControlStateNormal];
    okBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [windowView addSubview:okBtn];
    
    [okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(windowView).offset(19);
        make.right.equalTo(windowView).offset(-19);
        make.top.equalTo(currentGlodLabel.mas_bottom).offset(26);
        make.height.mas_equalTo(40);
    }];
    
    UILabel *tipLabel = [UILabel new];
    tipLabel.text = @"病历购买后将置入病历收藏里";
    tipLabel.textColor = GloballightFontColor;
    tipLabel.font = [UIFont systemFontOfSize:13];
    [tipLabel sizeToFit];
    [windowView addSubview:tipLabel];
    
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(windowView);
        make.top.equalTo(okBtn.mas_bottom).offset(15);
    }];
}

-(void)okBtnDidClick
{
    if (self.recordgold <= self.userGlod)
    {
        //        通知阅读量
        NSString *url = @"record/readRecord.do";
        NSDictionary *dict = @{@"recordId":@(self.selectedModel.id)};
        
        [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if([responseObject[@"result"] isEqualToString:@"success"])
            {
                [self coverBtnDidClick];
                
                [self showSuccessInfoWithStatus:@"付费成功"];
                
                self.hidesBottomBarWhenPushed = YES;
                WPReadingInvidialRanking *rirVC = [WPReadingInvidialRanking new];
                rirVC.model = self.selectedModel;
                [self.navigationController pushViewController:rirVC animated:YES];
                self.hidesBottomBarWhenPushed = NO;
            }
            else
            {
                
                //            考虑到账号过期的情况
                if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
                {
                    //                账号过期
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                    [alertView show];
                }
                else
                {
                    [self showErrorInfoWithStatus:@"付费失败,请稍后重试"];
                }
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [self showErrorInfoWithStatus:@"网络错误"];
            
        }];
    }
    else
    {
        //        跳转到购买金币界面
        [self coverBtnDidClick];
        
        //        调到购买金币界面去购买金币
        self.hidesBottomBarWhenPushed = YES;
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WPGoldCoinVC" bundle:nil];
        WPCoinTopUp *gcVC = [sb instantiateViewControllerWithIdentifier:@"goldTopUp"];
        [self.navigationController pushViewController:gcVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
        
        //        重新比较一下金币数量
        gcVC.backBlock = ^
        {
            [self famousCellCellDidClick:nil model:self.selectedModel];
        };
    }
}

#pragma mark - WPReadingViewCollectionViewCellVCDelegate的代理方法
-(void)invidialCaseCellDidClickWith:(WPReadingViewCollectionViewCellVC *)cell model:(WPMRModel *)model
{
        self.hidesBottomBarWhenPushed = YES;
        WPReadingInvidialRanking *rirVC = [WPReadingInvidialRanking new];
        rirVC.model = model;
        [self.navigationController pushViewController:rirVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
}

-(void)invidialCaseCellDidClickDiseaseWith:(WPReadingViewCollectionViewCellVC *)vc disease:(NSString *)disease
{
        self.hidesBottomBarWhenPushed = YES;
        WPNewDiseaseInstructionView *rirVC = [WPNewDiseaseInstructionView new];
        rirVC.disease = disease;
        [self.navigationController pushViewController:rirVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
}

#pragma mark - WPReadingViewCollectionViewChargeCellVCDelegate

-(void)chargeCaseCellDidClickWith:(WPReadingViewCollectionViewChargeCellVC *)vc model:(WPMRModel *)model
{
    self.selectedModel = model;
    
    if ([WPUserAccountViewModel sharedInstance].userLogin)
    {
        NSString *url = @"record/getGoldNum.do";
        NSDictionary *dict = @{@"recordId":@(model.id)};
        
        [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if ([responseObject[@"result"] isEqualToString:@"success"])
            {
                if ([responseObject[@"detail"][@"status"] intValue])
                {
                    
                    NSString *url = @"record/readRecord.do";
                    NSDictionary *dict = @{@"recordId":@(self.selectedModel.id)};
                    
                    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
                        
                    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                        
                    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                        
                    }];
                    
                    
                    self.hidesBottomBarWhenPushed = YES;
                    WPReadingInvidialRanking *rirVC = [WPReadingInvidialRanking new];
                    rirVC.model = model;
                    [self.navigationController pushViewController:rirVC animated:YES];
                    self.hidesBottomBarWhenPushed = NO;
                }
                else
                {
                    self.recordgold = [responseObject[@"detail"][@"recordGold"] intValue];
                    self.userGlod = [responseObject[@"detail"][@"userGold"] intValue];
                    
                    [self showGoldInfomation];
                }
            }
            else
            {
                //            考虑到账号过期的情况
                if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
                {
                    //                账号过期
                    [self illegalLoginWithSuccessBlock:^{
                        
                        WPMineLoginVC *vc = [WPMineLoginVC new];
                        [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
                    }];
                }
                else
                {
                    [self showErrorInfoWithStatus:@"网络错误"];
                }
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [self showErrorInfoWithStatus:@"网络错误"];
        }];
    }
    else
    {
        [[WPUserAccountViewModel sharedInstance] loginTipWith:^{
            
            WPMineLoginVC *vc = [WPMineLoginVC new];
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
        }];
    }
}

-(void)chargeCaseCellDidClickDiseaseWith:(WPReadingViewCollectionViewChargeCellVC *)vc disease:(NSString *)disease
{
    if ([WPUserAccountViewModel sharedInstance].userLogin)
    {
        self.hidesBottomBarWhenPushed = YES;
        WPNewDiseaseInstructionView *rirVC = [WPNewDiseaseInstructionView new];
        rirVC.disease = disease;
        [self.navigationController pushViewController:rirVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }
    else
    {
        [[WPUserAccountViewModel sharedInstance] loginTipWith:^{
            
            WPMineLoginVC *vc = [WPMineLoginVC new];
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
        }];
    }
}


#pragma mark - 按钮点击相应事件
-(void)searchBtnDidClick
{
    WPSearchVC *vc = [WPSearchVC new];
    vc.isFiveDiseases = YES;
    [self.navigationController pushViewController:vc animated:NO];
}

-(void)coverBtnDidClick
{
    [self.windowView removeFromSuperview];
    [self.coverBtn removeFromSuperview];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger indexPage = scrollView.contentOffset.x/scrollView.bounds.size.width;
    
    if (indexPage == 0)
    {
        [self titleBtnDidClick:self.indivalBtn];
    }
    else
    {
        [self titleBtnDidClick:self.famousBtn];
    }
}

-(WPBtnNoHighLight *)buttonWith:(NSString *)title
{
    WPBtnNoHighLight *btn = [WPBtnNoHighLight buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:GloballightFontColor forState:UIControlStateNormal];
    [btn setTitleColor:GLobalFontColor forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(titleBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    return btn;
}

-(void)titleBtnDidClick:(WPBtnNoHighLight *)btn
{
    if (self.currentBtn == btn)
    {
        return ;
    }
    self.currentBtn.selected = NO;
    self.currentBtn = btn;
    self.currentBtn.selected = YES;
    
    if (btn == self.indivalBtn)
    {
        [UIView animateWithDuration:sliderViewAnimationTime animations:^{
            
            self.sliderView.transform = CGAffineTransformIdentity;
        }];
        
        [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    else
    {
        [UIView animateWithDuration:sliderViewAnimationTime animations:^{
            
            self.sliderView.transform = CGAffineTransformMakeTranslation(CGRectGetMinX(self.famousBtn.frame)-self.averageWidth, 0);
        }];
        
        [self.collectionView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
    }
}

-(void)dealloc
{
//    NSLog(@"疾病类型控制器死了");
}

-(void)backBtnDidClick
{
    if (self.isPop)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}
@end
